﻿using MailKit.Net.Smtp;
using MimeKit;

namespace NetCoreConsole1.Tools
{
    public static class EmailHelper
    {
        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="subject">邮件标题</param>
        /// <param name="content">邮件内容</param>
        /// <param name="to">收件人</param>
        public static void SendEmial(string subject, string content, string to)
        {
            try
            {
                string emailFromAddress = "z13034679@126.com";
                string emailFromPassword = "GZSdt6TDQ34Rr2Nj";
                string emailHostIP = "smtp.126.com";
                string emailHostPort = "465";
                MimeMessage mes = new MimeMessage();
                //发件人
                mes.From.Add(new MailboxAddress("webhook", emailFromAddress));
                //收件人
                mes.To.Add(new MailboxAddress(to, to));
                //标题
                mes.Subject = subject;
                TextPart text = new TextPart(MimeKit.Text.TextFormat.Html)
                {
                    //邮件内容
                    Text = content
                };
                Multipart multipart = new MimeKit.Multipart("mixed");
                multipart.Add(text);
                mes.Body = multipart;
                using (SmtpClient client = new SmtpClient())
                {
                    //邮件服务器
                    client.Connect(emailHostIP, Convert.ToInt32(emailHostPort), true);
                    //帐号、密码
                    client.Authenticate(emailFromAddress, emailFromPassword);
                    client.Send(mes);
                    client.Disconnect(true);
                }
            }
            catch (Exception ex)
            {

            }

        }
    }

}
